package tests.tika;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.net.URL;
import java.util.Arrays;

import org.apache.tika.detect.DefaultDetector;
import org.apache.tika.detect.TypeDetector;
import org.apache.tika.io.TikaInputStream;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.mime.MediaType;
import org.apache.tika.mime.MimeTypes;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.parser.PasswordProvider;
import org.apache.tika.sax.WriteOutContentHandler;
import org.nanotek.lucene.util.StringBufferOutputStream;
import org.xml.sax.ContentHandler;
import org.xml.sax.helpers.DefaultHandler;

public class MediaDetectorLoader {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		TypeDetector   mt = new TypeDetector  (); 
		try {
			
			ParseContext context = new ParseContext();
			DefaultDetector detector = new DefaultDetector();
			AutoDetectParser parser = new AutoDetectParser(detector);
			context.set(Parser.class, parser);
			context.set(PasswordProvider.class, new PasswordProvider() {
				public String getPassword(Metadata metadata) {
					String password = "";
					return password ;
				}
			});
			File file = new File("/home/java-eclipse/workspace/MusicBrainzGoogleImporter/src/tests/tika/MediaDetectorLoader.java");
			URL url  = file.toURI().toURL();
            Metadata metadata = new Metadata();
            InputStream input = TikaInputStream.get(url, metadata);
			handleStream(input,metadata);
			System.out.println(parser.getMediaTypeRegistry());
		} catch ( Exception e) {
			e.printStackTrace();
		}

	}

	private static void handleStream(InputStream input, Metadata md) throws Exception {

        StringBuilder metadataBuffer = new StringBuilder();

		ParseContext context = new ParseContext();
		DefaultDetector detector = new DefaultDetector();
		AutoDetectParser parser = new AutoDetectParser(detector);
		context.set(Parser.class, parser);
		context.set(PasswordProvider.class, new PasswordProvider() {
			public String getPassword(Metadata metadata) {
				String password = "";
				return password ;
			}
		});
        StringBuffer outBuffer = new StringBuffer();
		AutoDetectParser txtParser = new AutoDetectParser(); 
		MimeTypes mt = new MimeTypes(); 
        Metadata metadata = new Metadata();
        MediaType mtd = mt.detect(input, new Metadata());
//        metadata.add(Metadata.CONTENT_TYPE, mediatype + "; charset=" + java.nio.charset.Charset.defaultCharset().name());
        StringBufferOutputStream out = new StringBufferOutputStream(outBuffer);
        OutputStreamWriter writer = new OutputStreamWriter (out , java.nio.charset.Charset.defaultCharset().name());
        ContentHandler handler = new WriteOutContentHandler(writer);
        
        parser.parse(input,  handler, md,  context);
        String[] names = md.names();
        Arrays.sort(names);
        for (String name : names) {
            metadataBuffer.append(name);
            metadataBuffer.append(": ");
            metadataBuffer.append(md.get(name));
            metadataBuffer.append("\n");
        }
//        txtParser.parse(stream, handler, metadata, context)
//        txtParser.parse(fis, handler, metadata, new ParseContext());
        out.flush(); 
        System.out.println(outBuffer.toString());
        
        System.out.println(metadataBuffer.toString());

    }
}
